package com.example.unique.ui.contact

import androidx.lifecycle.viewModelScope
import android.annotation.SuppressLint
import android.database.Cursor

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.unique.database.DatabaseJob
import com.example.unique.database.DatabaseTaskCompletion
//    val sno:String,
//     sno =cursor.getString(cursor.getColumnIndex("completorSno"))?:"",
data class Complete(

    val qrCodeUri:String,
    val id: Int,
    val club: String,
    val startTime: String,
    val endTime: String,
    val target: String,
    val radius: Double,
    val jobStatus: Int,
    val taskName: String
)

class ContactViewModel(private val jobDatabase:DatabaseJob,private val taskCompletion: DatabaseTaskCompletion) : ViewModel() {

    private val _completeInfo=MutableLiveData<Complete?>()
    val completeInfo:LiveData<Complete?> =_completeInfo

    @SuppressLint("Range")
    fun getTargetJobInfo(club:String, taskName:String){
        val cursor:Cursor=jobDatabase.getTargetJob(club,taskName)
        if(cursor.moveToFirst()){
            val complete =Complete(
                qrCodeUri = cursor.getString(cursor.getColumnIndex("qrCodeUri")),
                id = cursor.getInt(cursor.getColumnIndex("id")),
                club = cursor.getString(cursor.getColumnIndex("club")),
                startTime = cursor.getString(cursor.getColumnIndex("startTime")),
                endTime = cursor.getString(cursor.getColumnIndex("endTime")),
                target = cursor.getString(cursor.getColumnIndex("target")),
                radius = cursor.getDouble(cursor.getColumnIndex("radius")),
                jobStatus = cursor.getInt(cursor.getColumnIndex("jobStatus")),
                taskName = cursor.getString(cursor.getColumnIndex("taskName"))
            )
            _completeInfo.value=complete
        }else{
            _completeInfo.value=null
        }
        cursor.close()
    }
    //得到某个指定任务的所以完成者信息    用于status建立图标
    private val _taskCompletorInfo = MutableLiveData<List<String>>()
    val taskCompletorInfo: LiveData<List<String>> = _taskCompletorInfo

    @SuppressLint("Range")
    fun getCompletorInfo(club: String, taskName: String){
        val cursor:Cursor=taskCompletion.getTaskCompletions(club,taskName)
        val completors = mutableListOf<String>()

        if (cursor.moveToFirst()) {
            do {
                if(cursor.getInt(cursor.getColumnIndex("isCompleted"))==1){
                    val sno = cursor.getString(cursor.getColumnIndex("completorSno"))
                    completors.add(sno)
                }
            } while (cursor.moveToNext())
        }
        cursor.close()
        // 更新 LiveData
        _taskCompletorInfo.value = completors
    }




}